package com.abel.roommanager.service.imp;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.abel.roommanager.mapper.LandlordMapper;
import com.abel.roommanager.mapper.RoomMapper;
import com.abel.roommanager.mapper.RoomTypeMapper;
import com.abel.roommanager.mapper.StudentMapper;
import com.abel.roommanager.pojo.Landlord;
import com.abel.roommanager.pojo.Room;
import com.abel.roommanager.pojo.RoomType;
import com.abel.roommanager.pojo.Student;
import com.abel.roommanager.service.RoomService;
import com.abel.roommanager.util.Page;

import lombok.Data;
@Service
@Data
public class RoomServiceImp implements RoomService{
	@Resource
    private RoomMapper roomMapper;
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private LandlordMapper landlordMapper;
	@Autowired
	private RoomTypeMapper roomTypeMapper;
	@Override
	public List<Room> findAllRoom() {
		// TODO Auto-generated method stub
		return roomMapper.findAllRoom();
	}
	@Override
	public List<Room> findRoomsBySid(int id) {
		//查询与当前学生相匹配的所有的房间（性别符合，房间没有住满）
//		根据学生id查询学生性别
		Student student=studentMapper.findStudentById(id);
		int sex=student.getSex();
//		根据学生性别查询出符合的宿舍
		List<Room> rooms=roomMapper.findRoomsByType(sex);
		return rooms;
	}
	@Override
	public Page<Room> findRoomsByCurpage(int curPage) {
		int count=3;
		int index=(curPage-1)*count;
		List<Room> data=roomMapper.findRoomsByCurPage(index,count);
		Page<Room> page=new Page<>();
		page.setData(data);
		page.setCount(count);
		page.setCurrentPage(curPage);
		page.setIndex(index);
		page.setNextPage(curPage+1);
		page.setPrevPage(curPage-1);
		int total=roomMapper.findCountRooms();
		page.setTotal(total);
		int totalSize=(total%count==0)?(total/count):(total/count+1);
		page.setTotalSize(totalSize);
		page.setUrl("jsp/room.jsp");
		return page;
	}
	@Override
	@Transactional
	public int addRoom(Room room, Landlord landlord) {
		int re=0;//添加失败
//      判断要插入的房间户型编号是否存在，存在才将户型编号插入到room表中。
		RoomType roomType=roomTypeMapper.findRoomTypeById(room.getRtid());
		if (roomType==null) {
//			不存在则返回
			return 1;//房间户型不存在，请重新选择！
		}
//		判断房东的信息是否已存在数据库中，
		Landlord landlord2=landlordMapper.findLandlordByLnameAndTel(landlord);
		if (landlord2!=null) {
//		已存在则查询该房东编号，把房东的编号插入room表中
//			房东信息已存在
			room.setLid(landlord2.getId());
		}else{
//		不存在则将房东信息插入房东表中，再将插入的房东编号返回，再将房东编号插入到room表中。
	     landlordMapper.addLandlord(landlord);
	     int lid=landlordMapper.findLastInsertId();
	     room.setLid(lid);
		}
		int rows=roomMapper.addRoom(room);
		if(rows>0){
			re=2;//添加成功
		}
		return re;
	}
	@Override
	public int delRoom(int id) {
		int re=0;//删除失败
//		先判断这个房间是否有人居住
		Room room=roomMapper.findRoomById(id);
		int livenumber=room.getLivenumber();
//		若有人居住不能删除
		if(livenumber>0){
			re=1;//当前房间有人居住，您不能删除。
		}else{
//		没人居住才能删除
			int rows=roomMapper.delRoom(id);
			if (rows>0) {
				re=2;
			}
		}
		return re;
	}
	@Override
	public Room findRoomInfoById(int id) {
		//通过房间编号查询房间详细信息
		return roomMapper.findRoomInfoById(id);
	}
	@Override
	public Page<Room> findRoomsByLimits(Room room, int enableLiving, int curPage) {
      //每页查询条数
		int count=3;
		//		从哪里开始查询
		int index=(curPage-1)*count;
//		查询符合条件的当前页的房间信息
		List<Room> rooms=roomMapper.findRoomsByLimits(room,enableLiving,index,count);
		Page<Room> pages=new Page<>();
		pages.setData(rooms);
		pages.setCurrentPage(curPage);
		pages.setNextPage(curPage+1);
		pages.setPrevPage(curPage-1);
//		查询符合条件的房间信息的总条数
		int total=roomMapper.findCountRoomByLimits(room,enableLiving);
		pages.setTotal(total);
//		总页码数
		int totalSize=(total%count)==0?(total/count):(total/count+1);
		pages.setTotalSize(totalSize);
		pages.setUrl("jsp/room.jsp");
		return pages;
	}
	@Override
	public List<Room> findRoomsBySex(int sex) {
		//根据性别查询房间未居住满的房间信息
		return roomMapper.findRoomsByType(sex);
	}
	@Override
	public List<Room> findRooms() {
		return roomMapper.findRooms();
	}

}
